package com.exampletest.service.impl;

import com.exampletest.mapper.MeetingMapper;
import com.exampletest.model.Meeting;
import com.exampletest.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MeetingServiceImpl implements MeetingService {
    @Autowired
    private MeetingMapper meetingMapper;

    @Override
    public int addMeeting(Meeting meeting) {
        return meetingMapper.insert(meeting);
    }

    @Override
    public int updateMeeting(Meeting meeting) {
        return meetingMapper.updateSelective(meeting);
    }

    @Override
    public int deleteMeeting(Integer id) {
        return meetingMapper.deleteById(id);
    }

    @Override
    public Meeting getMeetingById(Integer id) {
        return meetingMapper.selectById(id);
    }

    @Override
    public List<Meeting> getAllMeetings() {
        return meetingMapper.selectAll();
    }

    @Override
    public List<Meeting> searchMeetings(String keyword, String startDate, String endDate, String status) {
        // 处理日期转换逻辑
        Date start = convertToDateTime(startDate);
        Date end = convertToDateTime(endDate);
        Integer statusInt = status != null ? Integer.parseInt(status) : null;
        return meetingMapper.searchMeetings(keyword, start, end, statusInt);
    }

    @Override
    @Transactional
    public void approveMeeting(Integer id, Integer status) {
        meetingMapper.updateIsApproved(id, status, "管理员", new Date());
    }

    // 修改日期转换方法
    Date convertToDateTime(String dateTimeStr) {
        if (dateTimeStr == null || dateTimeStr.isEmpty()) {
            return null;
        }
        try {
            // 支持ISO格式：yyyy-MM-ddTHH:mm
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            return sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            throw new IllegalArgumentException("日期时间格式错误，应为yyyy-MM-ddTHH:mm", e);
        }
    }

    @Override
    public Page<Meeting> searchMeetingsWithPage(
            String keyword, String startDate, String endDate,
            Pageable pageable
    ) {
        // 处理日期转换逻辑
        Date start = convertToDateTime(startDate);
        Date end = convertToDateTime(endDate);

        // 计算分页偏移量
        int offset = (int) pageable.getOffset();
        int limit = pageable.getPageSize();

        // 获取当前页数据
        List<Meeting> meetings = meetingMapper.searchMeetingsWithPage(
                keyword, start, end, offset, limit
        );

        // 获取总记录数
        long totalItems = meetingMapper.countMeetings(keyword, start, end);

        // 创建Spring Data Page对象
        return new PageImpl<>(meetings, pageable, totalItems);
    }
}